﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Agile_E
{
    public partial class ManagerInv : System.Web.UI.Page
    {
        private string make;
        private string model;
        private string from = "inv";
        private string sqlquery;
        private bool stop = false;
        private string pricelow, pricehigh;
        private string mileslow, mileshigh;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                Session["From"] = from;
        }

        protected void ddlmake_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlmodel.Items.Clear();
            ddlmodel.Items.Insert(0, "--SELECT--");
            ddlmodel.SelectedIndex = 0;
            srcdllmodel.SelectCommand = "Select distinct(Model) From Vehicles where Make = '" + ddlmake.SelectedItem.Text + "'";
        }

        protected void btncarsearch_Click(object sender, EventArgs e)
        {
            make = ddlmake.SelectedItem.Text;
            model = ddlmodel.SelectedItem.Text;
            Session["Make"] = make;
            Session["Model"] = model;
            //Session["VID"];
            if (ddlmake.SelectedIndex > 0 && ddlmodel.SelectedIndex == 0)
                sqlquery = "Select * from Vehicles left join Vehicle_Status on Vehicles.S_ID = Vehicle_Status.S_ID " +
                    "where make = '" + make + "' ";

            else if (ddlmake.SelectedIndex > 0 && ddlmodel.SelectedIndex > 0)
                sqlquery = "Select * from Vehicles left join Vehicle_Status on Vehicles.S_ID = Vehicle_Status.S_ID "
                    + "where make = '" + make +
                    "' and model = '" + model + "' ";
            else
                sqlquery = "Select * from Vehicles left join Vehicle_Status on Vehicles.S_ID = Vehicle_Status.S_ID ";

            if (txtpricelow.Text != string.Empty && txtpricehigh.Text != string.Empty)
            {
                if (Convert.ToDouble(txtpricehigh.Text) < Convert.ToDouble(txtpricelow.Text))
                {
                    stop = true;
                    ((Main)((SearchMP)this.Master).Master).writeStatus("Max price cannot be lower than minimum price", true);
                }
                else
                {
                    stop = false;
                }
            }
            if (txtmileslow.Text != string.Empty && txtmileshigh.Text != string.Empty)
            {
                if (Convert.ToInt32(txtmileshigh.Text) < Convert.ToInt32(txtmileslow.Text))
                {

                    ((Main)((SearchMP)this.Master).Master).writeStatus("Max mileage cannot be lower than minimum miles", true);
                    stop = true;
                }
                else
                    stop = false;
            }
            if (stop == false)
            {
                if (txtpricelow.Text == string.Empty)
                    pricelow = string.Empty;
                else
                {
                    pricelow = txtpricelow.Text;
                    sqlquery += "and msrp > " + pricelow + " ";
                }
                if (txtpricehigh.Text == string.Empty)
                    pricehigh = string.Empty;
                else
                {
                    pricehigh = txtpricehigh.Text;
                    sqlquery += "and msrp < " + pricehigh + " ";
                }

                if (txtmileslow.Text == string.Empty)
                    mileslow = string.Empty;
                else
                {
                    mileslow = txtmileslow.Text;
                    sqlquery += "and mileage > " + mileslow + " ";
                }
                if (txtmileshigh.Text == string.Empty)
                    mileshigh = string.Empty;
                else
                {
                    mileshigh = txtmileshigh.Text;
                    sqlquery += "and mileage < " + mileshigh + " ";
                }
            }
            try
            {
                srcVehicles.SelectCommand = sqlquery;
                ((Main)this.Master).clearStatus();
                grdCar.DataBind();
            }
            catch (Exception ex)
            {
                ((Main)this.Master).writeStatus(ex.Message, true);
            }
        }
    }
}
